//
//  WNProcessingTime.m
//  WeiNingProject
//
//  Created by tianjians on 2018/10/25.
//  Copyright © 2018年 卫宁数据. All rights reserved.
//

#import "WNProcessingTime.h"

@implementation WNProcessingTime
//NSDate转时间戳
+(NSString *)timeStampWithDate:(NSDate *)date{
    NSString *timeStamp = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
    return timeStamp;
}
//时间戳转换为时间方法
+(NSString *)dateStringWithTimeStamp:(NSString *)timeStamp andFormatString:(NSString *)formatString{
    NSString *dateString;
    NSDate *tmpDate = [NSDate dateWithTimeIntervalSince1970:[timeStamp floatValue]];
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:formatString];
    dateString = [format stringFromDate:tmpDate];
    return dateString;
}
//格式化NSDate
+(NSString *)dateStringWithDate:(NSDate *)date andFormatString:(NSString *)formatString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatString];
    NSString *dateString = [dateFormatter stringFromDate:date];
//    NSLog(@"dateString:%@",dateString);
    return dateString;
}
//获取当前时间并进行格式化
+(NSString *)getCurrentDateWithFormatString:(NSString *)formatString{
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSString *currentDateString = [self dateStringWithDate:currentDate andFormatString:formatString];
    return currentDateString;
}
//将时间字符串转换成NSDate格式
+(NSDate *)dateWithDateString:(NSString *)dateString andFormatString:(NSString *)formatString{
    NSDate *tmpDate;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatString];
    tmpDate = [dateFormatter dateFromString:dateString];
    return tmpDate;
}

+(NSString *)getTodayWeekdayWithFormatString:(NSString *)formatString withDate:(NSDate *)date
{
    NSDate *currentDate = date;//获取当前时间，日期
    NSString *currentDateString = [self dateStringWithDate:currentDate andFormatString:formatString];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekday) fromDate:currentDate];
    NSString *week;
    NSInteger weekday = [comps weekday];
    switch (weekday) {
        case 1:
            week = @"日";
            break;
        case 2:
            week = @"一";
            break;
        case 3:
            week = @"二";
            break;
        case 4:
            week = @"三";
            break;
        case 5:
            week = @"四";
            break;
        case 6:
            week = @"五";
            break;
        default:
             week = @"六";
            break;
    }
    return [NSString stringWithFormat:@"%@ 周%@",currentDateString,week];
}



+(NSString *)getTodayWeekdayWithFormatString:(NSString *)formatString
{
    NSDate *currentDate = [self dateWithDateString:formatString andFormatString:@"yyyy年MM月dd日"];//获取当前时间，日期
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekday) fromDate:currentDate];
    NSString *week;
    NSInteger weekday = [comps weekday];
    switch (weekday) {
        case 1:
            week = @"周日";
            break;
        case 2:
            week = @"周一";
            break;
        case 3:
            week = @"周二";
            break;
        case 4:
            week = @"周三";
            break;
        case 5:
            week = @"周四";
            break;
        case 6:
            week = @"周五";
            break;
        default:
            week = @"周六";
            break;
    }
    return week;
}

+(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    return mDate;
    
}

+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {//降
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){//升
        //NSLog(@"Date1 is in the past");   oneDay < anotherDay
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
}


+(NSString *)current:(NSString *)current withAddMoreDay:(NSInteger)day
{
//    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
//    NSDateComponents *dateCom = [[NSDateComponents alloc] init];
//    dateCom.year = 0;
//    NSDate *maxdate = [calendar dateByAddingComponents:dateCom toDate:[NSDate date] options:0];
//
//    NSDateComponents *miniDateCom = [[NSDateComponents alloc] init];
//    miniDateCom.year = -100;
//    NSDate *miniDate = [calendar dateByAddingComponents:miniDateCom toDate:[NSDate date] options:0];
    
    
    NSDate *date = [self dateWithDateString:current andFormatString:@"yyyy年MM月dd日"];
    NSDate *lastDate = [NSDate dateWithTimeInterval:day * 24 * 60 * 60 sinceDate:date];
    NSString *str = [self dateStringWithDate:lastDate andFormatString:@"yyyy年MM月dd日"];
    return str;
}





+(NSString *)currentTimeAdd:(NSInteger)number history_CreatDate:(NSDate*)newDate{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmp = [calendar components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:newDate];
    if(number){
        [cmp setMonth:[cmp month] + number ];
    }
    
    NSDate *date = [calendar dateFromComponents:cmp];
    return [self dateStringWithDate:date andFormatString:@"yyyy-MM"];

}

+(NSString *)getCurrentWeekTimeToYearAndMonthDay:(NSInteger )currentIndex
{
   //获取7天后的日期
   NSInteger days =  currentIndex * 7;    // n天后的天数
   NSTimeInterval oneDay = 24 * 60 * 60;  // 一天一共有多少秒
   NSDate *appointDate = [[NSDate date] initWithTimeIntervalSinceNow: oneDay * days];
    
   
   NSCalendar *calendar = [NSCalendar currentCalendar];

   NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:appointDate];

   // 获取今天是周几

   NSInteger weekDay = [comp weekday];
   
   // 获取几天是几号

   NSInteger day = [comp day];
    
   // 计算当前日期和本周的星期一和星期天相差天数

   long firstDiff,lastDiff;

   if (weekDay == 1)

   {

       firstDiff = -6;

       lastDiff = 0;

   }

   else

   {
       firstDiff = [calendar firstWeekday] - weekDay + 1;
       lastDiff = 8 - weekDay;
   }

   // 在当前日期(去掉时分秒)基础上加上差的天数

   NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:appointDate];

   [firstDayComp setDay:day + firstDiff];

   NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];


   NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:appointDate];

   [lastDayComp setDay:day + lastDiff];

   NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];

   

   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

   [formatter setDateFormat:@"yyyy.MM.dd"];

   NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];

   NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];

    NSString *dateStr = [NSString stringWithFormat:@"%@ — %@",firstDay,lastDay];

    return dateStr;
}

@end
